//
//  AddDetailController.m
//  敢唆嘛!
//
//  Created by lanou on 15/11/25.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "AddDetailController.h"
#import "Device.h"
#import "Define.h"
#import <BmobSDK/BmobProFile.h>

#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/ALAssetsLibrary.h>
#import <AssetsLibrary/ALAssetsGroup.h>
#import <AssetsLibrary/ALAssetRepresentation.h>



typedef NS_ENUM(NSInteger, imageViewState){
    coverImageView,// 点了封面添加图片
    articlesImageView// 点了文章添加图片
};




@interface AddDetailController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
{
    imageViewState selectState;
}
@property(nonatomic , strong)UIView *bottomView;//底部弹出相机相册按钮
@property(nonatomic , strong)UILabel *coverLabel;//顶部标题（请添加封面图片）
@property(nonatomic , strong)UIView *lineView;//灰色线条

@property(nonatomic , strong)UIView *channelView;//频道选择视图
@property(nonatomic , strong)UILabel *choseLabel;//频道选择框
@property(nonatomic , strong)UIView *shadowView;//阴影视图

@property(nonatomic , strong)UIImageView *greenCircle;//绿色小圈
@property(nonatomic , strong)UILabel *realityLabel;//真事
@property(nonatomic , strong)UIImageView *realityClose;//灰色勾
@property(nonatomic , strong)UIImageView *realityOpen;//绿色勾

@property(nonatomic , strong)UIImageView *orangeCircle;//橙色色小圈
@property(nonatomic , strong)UILabel *creationLabel;//创作
@property(nonatomic , strong)UIImageView *creationClose;//灰色勾
@property(nonatomic , strong)UIImageView *creationOpen;//绿色勾

@property(nonatomic , strong)UIImageView *blueCircle;//蓝色小圈
@property(nonatomic , strong)UILabel *travelLabel;//游记
@property(nonatomic , strong)UIImageView *travelClose;//灰色勾
@property(nonatomic , strong)UIImageView *travelOpen;//绿色勾

@property(nonatomic , strong)UIImageView *redCircle;//绿色小圈
@property(nonatomic , strong)UILabel *secretLabel;//真事
@property(nonatomic , strong)UIImageView *secretClose;//灰色勾
@property(nonatomic , strong)UIImageView *secretOpen;//绿色勾

@property(nonatomic , copy)NSString *imageName;
@property(nonatomic , strong)UIButton *saveBtn;
@end

@implementation AddDetailController
{
    NSInteger  oldRandomNum;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
//    设置返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, KSWidth/18, KSWidth/18);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-unie61e"] forState:UIControlStateNormal];
    //添加方法
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backBarBtn;
 
//    设置发布按钮
    self.saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveBtn.frame = CGRectMake(KSWidth-space*3, 0, space*3, space);
    UILabel *saveTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, space*3, space)];
    saveTitle.text = @"发布";
    saveTitle.textColor = [UIColor colorWithRed:80/255.0 green:80/255.0  blue:80/255.0  alpha:1.0];
    saveTitle.textAlignment = NSTextAlignmentCenter;
    [self.saveBtn addSubview:saveTitle];
    //添加方法
    [self.saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *saveBarBtn = [[UIBarButtonItem alloc]initWithCustomView:self.saveBtn];
    self.navigationItem.rightBarButtonItem = saveBarBtn;
    
    self.coverLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, space*2, KSWidth, space)];
    self.coverLabel.text = @"请添加封面图片";
    self.coverLabel.textAlignment = NSTextAlignmentCenter;
    self.coverLabel.textColor = [UIColor grayColor];
    self.coverLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:self.coverLabel];
    
//    封面图片
    self.coverImageView = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4, space*4, KSWidth/2, KSWidth/2)];
    self.coverImageView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0  blue:245 / 255.0 alpha:1];
    self.coverImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.coverImageView];
    
//    标题
    self.titleField = [[UITextField alloc]initWithFrame:CGRectMake(space*3, KSWidth/2+space*6, KSWidth-space*6, space*1.5)];
    self.titleField.delegate = self;
    self.titleField.placeholder = @"请输入标题";
    self.titleField.font = [UIFont systemFontOfSize:15];
    self.titleField.textColor = [UIColor blackColor];
    [self.view addSubview:self.titleField];
    
//    灰色线条
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake(space*3, KSWidth/2+space*7.5, KSWidth-space*6, 1)];
    self.lineView.backgroundColor = [UIColor grayColor];
    self.lineView.alpha = 0.4;
    [self.view addSubview:self.lineView];
    
    self.choseLabel = [[UILabel alloc]initWithFrame:CGRectMake(space*3, KSWidth/2+space*8.5, KSWidth-space*6, space*2)];
    self.choseLabel.userInteractionEnabled = YES;
    [self.choseLabel.layer setCornerRadius:15];
    [self.choseLabel.layer setMasksToBounds:YES];//设置边框可见
    self.choseLabel.layer.borderColor = [[UIColor grayColor] CGColor];
    [self.choseLabel.layer setBorderWidth:0.8];
    
//    小圆圈
    self.circleImageView =[[UIImageView alloc]initWithFrame:CGRectMake(space, space/2,space, space)];
    self.circleImageView.image = [UIImage imageNamed:@"iconfont-yuanquanGray"];
    [self.choseLabel addSubview:self.circleImageView];
    
//    标签
    self.tagLabel = [[UILabel alloc]initWithFrame:CGRectMake(space*3, space/2, space*4, space)];
    self.tagLabel.textColor = [UIColor grayColor];
    self.tagLabel.text = @"选择频道";
    self.tagLabel.font = [UIFont systemFontOfSize:12];
    [self.choseLabel addSubview:self.tagLabel];
    [self.view addSubview:self.choseLabel];
    
    
    //    视图下部中间灰色线条
    UIView *lineUnderView = [[UIView alloc]initWithFrame:CGRectMake(KSWidth/2, KSWidth, 1, KSWidth/2)];
    lineUnderView.backgroundColor = [UIColor grayColor];
    lineUnderView.alpha = 0.3;
    [self.view addSubview:lineUnderView];
    
    //   添加一张文章图片
    UILabel *wzLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, KSWidth, KSWidth/2, space)];
    wzLabel.textAlignment = NSTextAlignmentCenter;
    wzLabel.textColor = [UIColor grayColor];
    wzLabel.text = @"添加一张文章图片";
    wzLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:wzLabel];
    
    //    文章图片
    self.articlesImageView = [[UIImageView alloc]initWithFrame:CGRectMake(space, KSWidth+space*2, KSWidth/2-space*2, KSWidth/2-space*2)];
    self.articlesImageView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0  blue:245 / 255.0 alpha:1];
    self.articlesImageView.userInteractionEnabled = YES;
    //    添加轻拍手势
    UITapGestureRecognizer *articlesTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.articlesImageView addGestureRecognizer:articlesTap];
    [self.view addSubview:self.articlesImageView];
    
    
    //    填写文章内容
    UILabel *nrLabel = [[UILabel alloc]initWithFrame:CGRectMake(KSWidth/2, KSWidth, KSWidth/2, space)];
    nrLabel.textAlignment = NSTextAlignmentCenter;
    nrLabel.textColor = [UIColor grayColor];
    nrLabel.text = @"添加文章内容";
    nrLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:nrLabel];
    
    //   文章内容显示Label
    self.articlesLabel = [[UILabel alloc]initWithFrame:CGRectMake(KSWidth/2+space, KSWidth+space*2, KSWidth/2-space*2, KSWidth/2-space*2)];
    self.articlesLabel.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0  blue:245 / 255.0 alpha:1];
    self.articlesLabel.numberOfLines = 0;
    self.articlesLabel.font = [UIFont systemFontOfSize:14];
    self.articlesLabel.userInteractionEnabled = YES;
    [self.view addSubview:self.articlesLabel];
    


//    相机相册弹出视图
    self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, KSHeight, KSWidth, KSHeight/3)];
    self.bottomView.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:250 / 255.0  blue:250 / 255.0 alpha:0.5];
    //    边框效果：
    [self.bottomView.layer setMasksToBounds:YES];//设置边框可见
//    self.bottomView.layer.borderColor = [[UIColor blackColor] CGColor];
//    [self.bottomView.layer setBorderWidth:0.8];
    //    阴影层：
    self.bottomView.layer.shadowOffset = CGSizeMake(0, 3);
    self.bottomView.layer.shadowRadius = 5.0;
    self.bottomView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.bottomView.layer.shadowOpacity = 0.8;
    
    UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, space*1.5, KSWidth, space)];
    tempLabel.text = @"选择图片来源";
    tempLabel.textColor = [UIColor grayColor];
    tempLabel.textAlignment = NSTextAlignmentCenter;
    tempLabel.font = [UIFont systemFontOfSize:12];
    [self.bottomView addSubview:tempLabel];
//    相册按钮
    UIButton *imageLibraryBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    imageLibraryBtn.frame = CGRectMake(0, space*3.5, KSWidth, space);
    imageLibraryBtn.tag = 100;
    [imageLibraryBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside ];
    
    UILabel *firstLabel = [[UILabel alloc]initWithFrame:CGRectMake((KSWidth-space*2)/2, 0, space*2, space)];
    firstLabel.textAlignment = NSTextAlignmentCenter;
    firstLabel.textColor = [UIColor blackColor];
    firstLabel.text = @"相册";
    firstLabel.font = [UIFont systemFontOfSize:12];
    [imageLibraryBtn addSubview:firstLabel];
    [self.bottomView addSubview:imageLibraryBtn];
    
//    相机按钮
    UIButton *photoBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    photoBtn.frame = CGRectMake(0, space*5.5, KSWidth, space);
    photoBtn.tag = 101;
    [photoBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside ];
    
    UILabel *secondLabel = [[UILabel alloc]initWithFrame:CGRectMake((KSWidth-space*2)/2, 0, space*2, space)];
    secondLabel.textAlignment = NSTextAlignmentCenter;
    secondLabel.textColor = [UIColor blackColor];
    secondLabel.text = @"相机";
    secondLabel.font = [UIFont systemFontOfSize:12];
    [photoBtn addSubview:secondLabel];
    [self.bottomView addSubview:photoBtn];
    
//    取消按钮
    UIButton *cancleBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    cancleBtn.frame = CGRectMake(0, space*8, KSWidth, space);
    cancleBtn.tag = 102;
    [cancleBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside ];
    
    UILabel *thirdLabel = [[UILabel alloc]initWithFrame:CGRectMake((KSWidth-space*2)/2, 0, space*2, space)];
    thirdLabel.textAlignment = NSTextAlignmentCenter;
    thirdLabel.textColor = [UIColor blackColor];
    thirdLabel.text = @"取消";
    thirdLabel.font = [UIFont systemFontOfSize:12];
    [cancleBtn addSubview:thirdLabel];
    [self.bottomView addSubview:cancleBtn];
    [self.view addSubview:self.bottomView];
    
    
//    封面图片轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.coverImageView addGestureRecognizer:tap];
    
    
//    选择频道视图
    self.channelView = [[UIView alloc]initWithFrame:CGRectMake(0, KSHeight, KSWidth, KSHeight/2)];
    self.channelView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0  blue:255 / 255.0 alpha:0.9];
    //    边框效果：
    [self.channelView.layer setMasksToBounds:YES];//设置边框可见
    self.channelView.layer.borderColor = [[UIColor cyanColor] CGColor];
    [self.channelView.layer setBorderWidth:0.5];
    //    阴影层：
    self.channelView.layer.shadowOffset = CGSizeMake(0, 3);
    self.channelView.layer.shadowRadius = 5.0;
    self.channelView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.channelView.layer.shadowOpacity = 0.8;
    
//    阴影视图
    self.shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, -KSHeight/2, KSWidth, KSHeight/2)];
    self.shadowView.backgroundColor = [UIColor blackColor];
    self.shadowView.alpha = 0.3;
//    self.shadowView.hidden = YES;
    [self.view addSubview:self.shadowView];
    
    UILabel *topLbel = [[UILabel alloc]initWithFrame:CGRectMake(0, space, KSWidth, space)];
    topLbel.textAlignment = NSTextAlignmentCenter;
    topLbel.text = @"选择频道";
    topLbel.font = [UIFont systemFontOfSize:14];
    [self.channelView addSubview:topLbel];
    
//    真事按钮
    UIButton *greenBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    greenBtn.frame = CGRectMake(0, space*3.5, KSWidth, space);
    greenBtn.tag = 200;
    [greenBtn addTarget:self action:@selector(channelAction:) forControlEvents:UIControlEventTouchUpInside];
//    小圈
    self.greenCircle = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4, 0, space, space)];
    self.greenCircle.image = [UIImage imageNamed:@"iconfont-yuanquan"];
    self.realityLabel = [[UILabel alloc]initWithFrame:CGRectMake(KSWidth/4+space*1.5, 0, space*4, space)];
    self.realityLabel.text = @"真事";
    self.realityLabel.textAlignment = NSTextAlignmentCenter;
    self.realityLabel.font = [UIFont systemFontOfSize:14];
//    选中状态
    self.realityOpen = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4*3, 0, space, space)];
    self.realityOpen.image = [UIImage imageNamed:@"iconfont-selectGreen"];
    self.realityOpen.hidden = YES;//先隐藏
//    未选中状态
    self.realityClose = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4*3, 0, space, space)];
    self.realityClose.image = [UIImage imageNamed:@"iconfont-select"];
    [greenBtn addSubview:self.realityClose];
    [greenBtn addSubview:self.realityOpen];
    [greenBtn addSubview:self.realityLabel];
    [greenBtn addSubview:self.greenCircle];
    [self.channelView addSubview:greenBtn];
    
 
//    创作按钮
    UIButton *orangeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    orangeBtn.frame = CGRectMake(0, space*6, KSWidth, space);
    orangeBtn.tag = 201;
    [orangeBtn addTarget:self action:@selector(channelAction:) forControlEvents:UIControlEventTouchUpInside];
    
//    小圈
    self.orangeCircle = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4, 0, space, space)];
    self.orangeCircle.image = [UIImage imageNamed:@"iconfont-yuanquan-3"];

    self.creationLabel = [[UILabel alloc]initWithFrame:CGRectMake(KSWidth/4+space*1.5, 0, space*4, space)];
    self.creationLabel.text = @"创作";
    self.creationLabel.textAlignment = NSTextAlignmentCenter;
    self.creationLabel.font = [UIFont systemFontOfSize:14];
    //    选中状态
    self.creationOpen = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4*3, 0, space, space)];
    self.creationOpen.image = [UIImage imageNamed:@"iconfont-selectGreen"];
    self.creationOpen.hidden = YES;//先隐藏
    //    未选中状态
    self.creationClose = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4*3, 0, space, space)];
    self.creationClose.image = [UIImage imageNamed:@"iconfont-select"];
    [orangeBtn addSubview:self.creationClose];
    [orangeBtn addSubview:self.creationOpen];
    [orangeBtn addSubview:self.creationLabel];
    [orangeBtn addSubview:self.orangeCircle];
    [self.channelView addSubview:orangeBtn];
    
//    游记按钮
    UIButton *blueBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    blueBtn.frame = CGRectMake(0, space*8.5, KSWidth, space);
    blueBtn.tag = 202;
    [blueBtn addTarget:self action:@selector(channelAction:) forControlEvents:UIControlEventTouchUpInside];
//    小圈
    self.blueCircle = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4, 0, space, space)];
    self.blueCircle.image = [UIImage imageNamed:@"iconfont-yuanquan-2"];
    
    self.travelLabel = [[UILabel alloc]initWithFrame:CGRectMake(KSWidth/4+space*1.5, 0, space*4, space)];
    self.travelLabel.text = @"游记";
    self.travelLabel.textAlignment = NSTextAlignmentCenter;
    self.travelLabel.font = [UIFont systemFontOfSize:14];
    //    选中状态
    self.travelOpen = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4*3, 0, space, space)];
    self.travelOpen.image = [UIImage imageNamed:@"iconfont-selectGreen"];
    self.travelOpen.hidden = YES;//先隐藏
    //    未选中状态
    self.travelClose = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4*3, 0, space, space)];
    self.travelClose.image = [UIImage imageNamed:@"iconfont-select"];
    [blueBtn addSubview:self.travelClose];
    [blueBtn addSubview:self.travelOpen];
    [blueBtn addSubview:self.travelLabel];
    [blueBtn addSubview:self.blueCircle];
    [self.channelView addSubview:blueBtn];
    
    
//    秘密按钮
    UIButton *redBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    redBtn.frame = CGRectMake(0, space*11, KSWidth, space);
    redBtn.tag = 203;
    [redBtn addTarget:self action:@selector(channelAction:) forControlEvents:UIControlEventTouchUpInside];
//    小圈
    self.redCircle = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4, 0, space, space)];
    self.redCircle.image = [UIImage imageNamed:@"iconfont-yuanquan-1"];
    
    self.secretLabel = [[UILabel alloc]initWithFrame:CGRectMake(KSWidth/4+space*1.5, 0, space*4, space)];
    self.secretLabel.text = @"秘密";
    self.secretLabel.textAlignment = NSTextAlignmentCenter;
    self.secretLabel.font = [UIFont systemFontOfSize:14];
    //    选中状态
    self.secretOpen = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4*3, 0, space, space)];
    self.secretOpen.image = [UIImage imageNamed:@"iconfont-selectGreen"];
    self.secretOpen.hidden = YES;//先隐藏
    //    未选中状态
    self.secretClose = [[UIImageView alloc]initWithFrame:CGRectMake(KSWidth/4*3, 0, space, space)];
    self.secretClose.image = [UIImage imageNamed:@"iconfont-select"];
    [redBtn addSubview:self.secretClose];
    [redBtn addSubview:self.secretOpen];
    [redBtn addSubview:self.secretLabel];
    [redBtn addSubview:self.redCircle];
    [self.channelView addSubview:redBtn];
    
//  确定按钮
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    sureBtn.frame = CGRectMake(0, space*14, KSWidth, space);
    sureBtn.tag = 204;
    [sureBtn addTarget:self action:@selector(channelAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titltLabel = [[UILabel alloc]initWithFrame:CGRectMake((KSWidth-space*5)/2, 0, space*5, space)];
    titltLabel.textAlignment = NSTextAlignmentCenter;
    titltLabel.textColor = [UIColor cyanColor];
    titltLabel.alpha = 0.8;
    titltLabel.text = @"确定";
    titltLabel.font = [UIFont systemFontOfSize:15];
    [sureBtn addSubview:titltLabel];
    [self.channelView addSubview:sureBtn];

    [self.view addSubview:self.channelView];
    
//    选择频道轻拍手势
    UITapGestureRecognizer *tapChannel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapChannelAction:)];
    [self.choseLabel addGestureRecognizer:tapChannel];
    
    
//   文章内容视图
    self.articlesView = [[UITextView alloc]initWithFrame:CGRectMake(space, KSHeight, KSWidth-space*2,  KSHeight/3-space*2)];
    self.articlesView.backgroundColor = [UIColor whiteColor];
    self.articlesView.font = [UIFont systemFontOfSize:17];
    self.articlesView.layer.borderColor = [UIColor lightGrayColor].CGColor ;
    self.articlesView.layer.borderWidth = 1.0;
    self.articlesView.layer.cornerRadius = 6.0;
    self.articlesView.layer.masksToBounds = YES;
    
    [self.view addSubview:self.articlesView];
//    弹出键盘上添加视图 用来点击回收键盘
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, KSWidth, space*2)];
    [topView setBarStyle:UIBarStyleBlack];
//    UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"未设事件" style:UIBarButtonItemStylePlain target:self action:nil];
//    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:doneButton,nil];
    [topView setItems:buttonsArray];
    [self.articlesView setInputAccessoryView:topView];
    
//文章内容视图轻拍手势
    UITapGestureRecognizer *tapArticlesView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapArticlesViewAction:)];
    [self.articlesLabel addGestureRecognizer:tapArticlesView];

//    收回相册 选择频道视图手势
    UITapGestureRecognizer *shadowTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shadowTapAction:)];
    [self.shadowView addGestureRecognizer:shadowTap];
    
    
}

//  阴影视图手势方法
-(void)shadowTapAction:(UITapGestureRecognizer *)tap
{
    self.coverImageView.userInteractionEnabled = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.shadowView.frame = CGRectMake(0, -KSHeight/2, KSWidth, KSHeight/2);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        self.channelView.frame = CGRectMake(0, KSHeight, KSWidth, KSHeight/2);
    }];
}




// 返回按钮
-(void)backBtnAction
{
    if (self.block != nil)
    {
        self.block(self.coverImageView.image,self.titleField.text,self.state);
    }
    
    
    [self.navigationController popViewControllerAnimated:YES];
}



//  封面轻拍手势事件方法
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    self.choseLabel.userInteractionEnabled = NO;
    CGPoint point = [tap locationInView:self.view];
//    NSLog(@"%f,%f",point.x,point.y);
    
    if (point.y<300)
    {
        selectState = coverImageView;
    }
    else if(point.y >300)
    {
        selectState = articlesImageView;
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomView.frame = CGRectMake(0, KSHeight/3*2, KSWidth, KSHeight/3);
    }];
    self.view.backgroundColor = [UIColor colorWithRed:252/ 255.0 green:252 / 255.0  blue:252 / 255.0 alpha:1];
}


//选择频道轻拍手势
-(void)tapChannelAction:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.25 animations:^{
        self.shadowView.frame = CGRectMake(0, 0, KSWidth, KSHeight/2);
    }];
    
    self.coverImageView.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.channelView.frame = CGRectMake(0, KSHeight/2, KSWidth, KSHeight/2);
    }];
}

// 文章内容轻拍手势
-(void)tapArticlesViewAction:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.25 animations:^{
        self.articlesView.frame = CGRectMake(space, KSWidth/3, KSWidth-space*2, KSHeight/3-space*2);
    }];
    //    添加键盘弹出的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    
    //    添加键盘回收的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}



// 选择频道按钮点击事件方法
-(void)channelAction:(UIButton *)btn
{
    self.realityClose.hidden = NO;
    self.realityOpen.hidden = YES;
    self.creationClose.hidden = NO;
    self.creationOpen.hidden = YES;
    self.travelClose.hidden = NO;
    self.travelOpen.hidden = YES;
    self.secretClose.hidden = NO;
    self.secretOpen.hidden = YES;
    self.channelView.subviews[1].backgroundColor = [UIColor clearColor];
    self.channelView.subviews[2].backgroundColor = [UIColor clearColor];
    self.channelView.subviews[3].backgroundColor = [UIColor clearColor];
    self.channelView.subviews[4].backgroundColor = [UIColor clearColor];
    if (btn.tag == 200)
    {
        self.state = chooseReality;
        self.channelView.subviews[1].backgroundColor = [UIColor whiteColor];
        self.realityClose.hidden = YES;
        self.realityOpen.hidden = NO;
    }
    else if(btn.tag == 201)
    {
        self.state = chooseCreation;
        self.channelView.subviews[2].backgroundColor = [UIColor whiteColor];
        self.creationClose.hidden = YES;
        self.creationOpen.hidden = NO;
    }
    else if(btn.tag == 202)
    {
        self.state = chooseTravel;
        self.channelView.subviews[3].backgroundColor = [UIColor whiteColor];
        self.travelClose.hidden = YES;
        self.travelOpen.hidden = NO;
    }
    else if(btn.tag == 203)
    {
        self.state = chooseSecret;
        self.channelView.subviews[4].backgroundColor = [UIColor whiteColor];
        self.secretClose.hidden = YES;
        self.secretOpen.hidden = NO;
    }
    else if(btn.tag == 204)
    {
        self.coverImageView.userInteractionEnabled = YES;
        [UIView animateWithDuration:0.25 animations:^{
            self.channelView.frame = CGRectMake(0, KSHeight, KSWidth, KSHeight/2);
        }];
        [UIView animateWithDuration:0.25 animations:^{
            self.shadowView.frame = CGRectMake(0, -KSHeight/2, KSWidth, KSHeight/2);
        }];
        if (self.state == chooseUnknow)
        {
            self.circleImageView.image = [UIImage imageNamed:@"iconfont-yuanquanGray"];
            self.tagLabel.text = @"选择频道";
        }
        else if (self.state == chooseReality)
        {
            self.circleImageView.image = [UIImage imageNamed:@"iconfont-yuanquan"];
            self.tagLabel.text = @"真事";
        }
        else if(self.state == chooseCreation)
        {
            self.circleImageView.image = [UIImage imageNamed:@"iconfont-yuanquan-3"];
            self.tagLabel.text = @"创作";
        }
        else if(self.state == chooseTravel)
        {
            self.circleImageView.image = [UIImage imageNamed:@"iconfont-yuanquan-2"];
            self.tagLabel.text = @"游记";
        }
        else if(self.state == chooseSecret)
        {
            self.circleImageView.image = [UIImage imageNamed:@"iconfont-yuanquan-1"];
            self.tagLabel.text = @"秘密";
        }
    }
}



//  相机相册按钮事件方法
-(void)btnAction:(UIButton *)btn
{
    if (btn.tag == 100)
    {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            UIImagePickerController * picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.allowsEditing = YES;//是否可以编辑
            //打开相册选择照片
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            // 模态进入相册
            [self presentViewController:picker animated:YES completion:nil];
        }
    }
    else if(btn.tag == 101)
    {
        // 如果有系统相机
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController * picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            //摄像头
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];
        }
        //如果没有系统相机提示用户
        else
        {
//            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"您的设备没有摄像头" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alertView show];
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的设备没有摄像头" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
            }];
            [alertController addAction:cancelAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
            
        }

    }
    else if(btn.tag == 102)
    {
        self.choseLabel.userInteractionEnabled = YES;
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.frame = CGRectMake(0, KSHeight, KSWidth, KSHeight/3);
        }];
    }
}

#pragma mark - 拍摄完成后或者选择相册完成后自动调用的方法 -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    
//    压缩 限制图片大小
    CGSize imageSizeOne = image.size;
    imageSizeOne.height = 256;
    imageSizeOne.width = 256;
    UIImage *imageOne = [self imageWithImage:image scaledToSize:imageSizeOne];
    
    CGSize imageSizeTwo = image.size;
    imageSizeTwo.height = 336;
    imageSizeTwo.width = 512;
    UIImage *imageTwo = [self imageWithImage:image scaledToSize:imageSizeTwo];
    
    //    随机赋图片名字
    long randomNum = arc4random() % 999;
    for (int i = 0; i < randomNum; i++)
    {
        if (oldRandomNum != randomNum)
        {
            break;
        }
        randomNum = arc4random() % 999;
        oldRandomNum = randomNum;
    }
    
    //得到图片
    if (selectState == coverImageView)
    {
        self.coverImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        self.coverImageName = [NSString stringWithFormat:@"coverImage-%ld.jpg",randomNum];
        [self saveImage:imageOne WithName:self.coverImageName];

    }
    else if (selectState == articlesImageView)
    {
        self.articlesImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        self.articlesImageName = [NSString stringWithFormat:@"articlesImage-%ld.jpg",randomNum];
        [self saveImage:imageTwo WithName:self.articlesImageName];

    }
    self.choseLabel.userInteractionEnabled = YES;
// 模态返回
    [self dismissViewControllerAnimated:YES completion:nil];
}

//压缩图片封装方法
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}

//保存图片封装方法
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
    NSData *imageData;
    if (UIImagePNGRepresentation(tempImage) == nil)
    {
        imageData = UIImageJPEGRepresentation(tempImage, 1);
    }
    else
    {
        imageData = UIImagePNGRepresentation(tempImage);
    }
//    NSData* imageData = UIImagePNGRepresentation(tempImage);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
}


#pragma mark === 键盘弹出的通知方法 ===
-(void)keyboardShow:(NSNotification *)notification
{
    //键盘弹出的时候 相当于 系统自己发送了一个post  无需我们自己去调用
    //    1.得到键盘所有信息
    NSDictionary *info = [notification userInfo];
//    NSLog(@"%@",info);
    //    2.得到键盘弹出来之后的frame
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    //    3.得到键盘的高度
    CGSize size = [value CGRectValue].size;
//    NSLog(@"键盘弹出＝＝%f",size.height);
    
    // 总的向上的偏移量
    CGFloat y = KSHeight-64-size.height-KSHeight/3+space*2;
    
    self.articlesView.frame = CGRectMake(space, y, KSWidth-space*2,  KSHeight/3-space*2);
//    space, space*2, KSWidth-space*2, KSWidth
}

#pragma mark === 键盘回收的通知方法 ===
-(void)keyboardHide:(NSNotification *)notification
{
    self.articlesView.frame =CGRectMake(space, space*8, KSWidth-space*2,  KSHeight/3-space*2);

}

//  回收键盘
-(void)dismissKeyBoard
{
    [self.articlesView resignFirstResponder];
    [UIView animateWithDuration:0.25 animations:^{
        self.articlesView.frame = CGRectMake(space, KSHeight, KSWidth-space*2, KSHeight/3-space*2);
    }];
    self.articlesLabel.text = self.articlesView.text;
//    移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

// 回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.titleField resignFirstResponder];
}


// 发布
-(void)saveBtnAction
{
    if (self.titleField.text.length == 0)
    {
        [self promptViewWithTitle:@"标题没写哟！"];
        
    }
    else if (self.coverImageView.image==nil)
    {
        [self promptViewWithTitle:@"封面图片没添加哟！"];
    }
    else if (self.articlesImageView.image == nil)
    {
        [self promptViewWithTitle:@"文章图片没添加哟！"];
    }
    else if (self.articlesView.text.length == 0)
    {
         [self promptViewWithTitle:@"文章内容还没写哟！"];
    }
    else if (self.state == chooseUnknow)
    {
        [self promptViewWithTitle:@"频道还没选哟"];
    }

    
    if (self.state == chooseReality)
    {
        [self saveDataWithClassName:@"Reality"];
    }
    else if (self.state == chooseCreation)
    {
        [self saveDataWithClassName:@"Creation"];
    }
    else if (self.state == chooseTravel)
    {
        [self saveDataWithClassName:@"Travel"];
    }
    else if (self.state == chooseSecret)
    {
        [self saveDataWithClassName:@"Secret"];
    }
    
    
}


// 保存数据
-(void)saveDataWithClassName:(NSString *)className
{
//    获取路径
    NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];;
    NSLog(@"%@",documentPath);
//    封面图片路径
    NSString *path1  = [NSString stringWithFormat:@"%@/%@",documentPath,self.coverImageName];
//    内容图片路径
    NSString *path2 = [NSString stringWithFormat:@"%@/%@",documentPath,self.articlesImageName];
    NSArray *array = @[path1,path2];
    
//批量上传文件
    [BmobProFile uploadFilesWithPaths:array resultBlock:^(NSArray *pathArray, NSArray *urlArray,NSArray *bmobFileArray,NSError *error) {
        //路径数组和url数组（url数组里面的元素为NSString）
        if (error) {
            NSLog(@"%@",error);
            if (self.coverImageView.image==nil)
            {
                [self promptViewWithTitle:@"封面图片没添加哟！"];
            }
            else if (self.articlesImageView.image == nil)
            {
                [self promptViewWithTitle:@"文章图片没添加哟！"];
            }
        }
        else
        {
            //路径数组和url数组（url数组里面的元素为NSString）
            NSLog(@"pathArray %@ urlArray %@",pathArray,urlArray);
            
            BmobObject *object = [BmobObject objectWithClassName:className];
            [object setObject:bmobFileArray[0] forKey:@"infoTitleImage"];
            [object setObject:bmobFileArray[1] forKey:@"infoImage"];
            [object setObject:self.titleField.text forKey:@"infoTitle"];
            [object setObject:self.tagLabel.text forKey:@"type"];
            [object setObject:self.articlesView.text forKey:@"info"];
            BmobUser *bmobUser = [BmobUser getCurrentUser];
            [object setObject:bmobUser.username forKey:@"userName"];
            [object setObject:[NSNumber numberWithInteger:0] forKey:@"recommendNumber"];
            [object setObject:[NSNumber numberWithInteger:0]  forKey:@"collectNumber"];
            [object setObject:[NSNumber numberWithInteger:0]  forKey:@"commentNumber"];
            [object setObject:[NSNumber numberWithInteger:0]  forKey:@"shareNumber"];
            [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error)
             {
                 if (isSuccessful)
                 {
                    NSLog(@"上传完成");
                     [self promptViewWithTitle:@"发布成功"];
                     // 延迟一秒
                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                         [UIView animateWithDuration:0.4 animations:^{
                             [self.navigationController popViewControllerAnimated:YES];
                                                     }completion:nil];
                     });

//                     如果上传完成 把document下的文件删除
                    NSFileManager *fileManager = [NSFileManager defaultManager];
                    [fileManager removeItemAtPath:path1 error:nil];
                    [fileManager removeItemAtPath:path2 error:nil];
                 }
                 else if(error)
                 {
                     NSLog(@"error === %@", error);
                     [self promptViewWithTitle:@"发布失败"];
                 }
             }];
            
        }
    } progress:^(NSUInteger index, CGFloat progress) {
        //index表示正在上传的文件其路径在数组当中的索引，progress表示该文件的上传进度
        NSLog(@"index %lu progress %f",(unsigned long)index,progress);

    }];
}

// 弹出提示窗口
- (void)promptViewWithTitle:(NSString *)title
{
//    动画开始 关必发布按钮的交互 以免动画窗口无限出来
    self.saveBtn.userInteractionEnabled = NO;
    UIView *promptView = [[UIView alloc] initWithFrame:CGRectMake(-(KSWidth - (KSWidth / 2)) / 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4)];
    promptView.backgroundColor = [UIColor whiteColor];
    promptView.alpha = 0;
    promptView.layer.cornerRadius = 10;
    promptView.layer.borderColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1.0].CGColor;
    promptView.layer.borderWidth = 1;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(-(KSWidth - (KSWidth / 2)) / 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4)];
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:KSWidth / 25];
    titleLabel.alpha = 0;
    [self.view addSubview:promptView];
    [self.view addSubview:titleLabel];
    
    [UIView animateWithDuration:0.4 animations:^{
        promptView.alpha = 0.8;
        titleLabel.alpha = 0.8;
        promptView.frame = CGRectMake((KSWidth - (KSWidth / 2)) / 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4);
        titleLabel.frame = CGRectMake((KSWidth - (KSWidth / 2)) / 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4);
    }completion:^(BOOL finished) {
        
        // 延迟一秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.4 animations:^{
                promptView.alpha = 0;
                titleLabel.alpha = 0;
                promptView.frame = CGRectMake(((KSWidth - (KSWidth / 2)) / 2) * 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4);
                titleLabel.frame = CGRectMake(((KSWidth - (KSWidth / 2)) / 2) * 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4);
                
            }completion:nil];
//            动画结束 开交互
        self.saveBtn.userInteractionEnabled = YES;
        });
    }];
}






@end
